package com.jing21.learn.codegenerator.util.baidu;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jing21.learn.codegenerator.PropertiesProvider;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class TransApi {

    public static void main(String[] args) {
/*        System.err.println(trans("activity participant"));
        System.err.println(trans("activityinfo"));
        System.err.println(trans("c shippingaddress"));
        System.err.println(trans("dict option"));
        System.err.println(trans("equipment debug data"));*/
        String str = "{\"from\":\"en\",\"to\":\"zh\",\"trans_result\":[{\"src\":\"activity participant\"," +
                "\"dst\":\"\\u6d3b\\u52a8\\u7684\\u53c2\\u4e0e\\u8005\"}]}\n" +
                "{\"from\":\"en\",\"to\":\"zh\",\"trans_result\":[{\"src\":\"activityinfo\"," +
                "\"dst\":\"activityinfo\"}]}\n" +
                "{\"from\":\"en\",\"to\":\"zh\",\"trans_result\":[{\"src\":\"c shippingaddress\",\"dst\":\"C " +
                "shippingaddress\"}]}\n" +
                "{\"from\":\"en\",\"to\":\"zh\",\"trans_result\":[{\"src\":\"dict option\"," +
                "\"dst\":\"\\u5b57\\u5178\\u7684\\u9009\\u62e9\"}]}\n" +
                "{\"from\":\"en\",\"to\":\"zh\",\"trans_result\":[{\"src\":\"equipment debug data\"," +
                "\"dst\":\"\\u8bbe\\u5907\\u8c03\\u8bd5\\u6570\\u636e\"}]}";
        String[] strings = str.split("\\n");
        Arrays.asList(strings).forEach(a -> {

        });

    }

    private static final String TRANS_API_HOST = "http://api.fanyi.baidu.com/api/trans/vip/translate";

    private String appid;
    private String securityKey;

    private static TransApi instans;

    public static String trans(String query) {
        if (instans == null) {
            instans = new TransApi(PropertiesProvider.getProperty("baidu.appid"),
                    PropertiesProvider.getProperty("baidu.secret"));
        }
        String transResult = instans.getTransResult(query, "en", "zh");
        if (transResult != null) {
            JSONObject jsonObject = JSONObject.parseObject(transResult);
            JSONArray result = jsonObject.getJSONArray("trans_result");
            if (transResult != null) {
                JSONObject object = result.getJSONObject(0);
                return (object.getString("dst"));
            }else {
                System.err.println("transResult erro===>"+ transResult);
            }
        }
        return null;
    }

    private TransApi(String appid, String securityKey) {
        this.appid = appid;
        this.securityKey = securityKey;
    }

    public String getTransResult(String query, String from, String to) {
        Map<String, String> params = buildParams(query, from, to);
        return HttpGet.get(TRANS_API_HOST, params);
    }

    private Map<String, String> buildParams(String query, String from, String to) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("q", query);
        params.put("from", from);
        params.put("to", to);

        params.put("appid", appid);

        // 随机数
        String salt = String.valueOf(System.currentTimeMillis());
        params.put("salt", salt);

        // 签名
        String src = appid + query + salt + securityKey; // 加密前的原文
        params.put("sign", MD5.md5(src));

        return params;
    }

}
